package com.qumitech.visualnovel.service.collect.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.ExcelUtils;
import com.qumitech.visualnovel.component.util.FileUtil;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.collect.CollectUserActiveDTO;
import com.qumitech.visualnovel.controller.vo.collect.CollectUserActiveReq;
import com.qumitech.visualnovel.controller.vo.collect.CollectUserActiveVO;
import com.qumitech.visualnovel.controller.vo.game.GameCollectReq;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.service.collect.CollectService;

import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/5/29 12:10
 */

@Service
public class CollectServiceImpl implements CollectService {


    private final String collect_user_active_url = "/admin/collect/user/active";
    private final String collect_wechatalarm_url = "/admin/collect/wechatalarm/list";


    @Override
    public Map<String, Object> getCollectUserActiveInfo(CollectUserActiveReq collectUserActiveReq) {

        return getCollectUserActiveInfoList(collectUserActiveReq);
    }

    @Override
    public Map<String, Object> exportMultipartFile(CollectUserActiveReq collectUserActiveReq, HttpServletResponse request) throws RuntimeException {



        Map<String, Object> rspMap = getCollectUserActiveInfoList(collectUserActiveReq);
        CollectUserActiveDTO collectUserActiveDTO = JSONObject.parseObject(JSONObject.toJSONString(rspMap), CollectUserActiveDTO.class);
        Map<String, Object> resultMap = new HashMap<>();
        try {

            List<CollectUserActiveVO> amounts = collectUserActiveDTO.getCollectUserActiveVOS();
            ExcelUtils<CollectUserActiveVO> xls = new ExcelUtils<CollectUserActiveVO>(CollectUserActiveVO.class);
            String filename = FileUtil.getExcelName();
            request.reset();
            request.setContentType("application/vnd.ms-excel;charset=utf-8");

            request.setHeader("Content-Disposition", "attachment;filename=" + filename);
            OutputStream outputStream = request.getOutputStream();
            xls.exportExcel("用户活动信息导出", amounts, outputStream);
            outputStream.flush();
            outputStream.close();
            resultMap.put("title", filename);
            return resultMap;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        throw new BusinessException(ResultEnum.EXCEL_EXPORT_ERROR);
    }

    @Override
    public Map<String, Object> getWeChatAlarmInfo(Integer offset, Integer limit, String startTime, String endTime) {
        String url = UrlConstant.SERVER_URL + collect_wechatalarm_url;
        Map<String, String> paramMap = new HashMap<>();
        if (offset != null)
            paramMap.put("offset", offset.toString());
        if (limit != null)
            paramMap.put("limit", limit.toString());
        if (startTime != null)
            paramMap.put("start_time", startTime);
        if (endTime != null)
            paramMap.put("end_time", endTime);

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    private Map<String, Object> getCollectUserActiveInfoList(CollectUserActiveReq collectUserActiveReq) {

        String url = UrlConstant.SERVER_URL + collect_user_active_url;
        String req = JSONObject.toJSONString(collectUserActiveReq);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;

    }


}
